//
//  PVDDetailSignatureTableViewCell.swift
//  GreenDeal
//
//  Created by Moennig Q on 10/2/2023.
//

import UIKit

class PVDDetailSignatureTableViewCell: UITableViewCell {
    
    lazy var backView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    let titleLabel = ContentLabel(R.font.interSemiBold(size: 16))
    
    lazy var subTitleLabel = ContentLabel(R.font.interRegular(size: 13), hexStr: "#292D32B2")
    
    lazy var resultLabel: BaseLabel = {
        let label = ContentLabel(R.font.interMedium(size: 14),hexStr: "#379657FF")
        label.numberOfLines = 0
        return label
    }()
    let signatureImageView = UIImageView()
    
    lazy var uploadFlagImageView: UIImageView = {
        let image = UIImageView()
        return image
    }()
    
    let isRequiredImageView = UIImageView(image: UIImage(named: "checkListCellStar"))
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpviews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    func setUpviews(){
        self.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.contentView.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.selectionStyle = .none
        //
        self.contentView.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.height.equalTo(BaseChecklistTableView.checklistCellHeight)
            make.top.bottom.equalToSuperview()
        }
        //type image view
        signatureImageView.image = R.image.jobdetail_signature_cell_sign_icon()
        backView.addSubview(signatureImageView)
        signatureImageView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.width.height.equalTo(30)
            make.centerY.equalToSuperview()
        }
        //uploadFlagImageView,是否上传成功的标记，红色感叹号是本地数据没有上传，绿色勾表示上传成功
        backView.addSubview(uploadFlagImageView)
        uploadFlagImageView.snp.makeConstraints { (make) in
            make.bottom.equalTo(signatureImageView)
            make.right.equalTo(signatureImageView).offset(3)
            make.size.equalTo(CGSize(width: 16, height: 11))
        }
        //isRequiredImageView
        backView.addSubview(isRequiredImageView)
        isRequiredImageView.snp.makeConstraints { make in
            make.top.equalTo(signatureImageView).offset(6)
            make.left.equalTo(signatureImageView.snp.right).offset(4)
            make.height.width.equalTo(7)
        }
        //contentBackView
        let contentBackView = UIView()
        contentBackView.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        backView.addSubview(contentBackView)
        contentBackView.snp.makeConstraints { make in
            make.left.equalTo(signatureImageView.snp.right).offset(12)
            make.centerY.equalTo(signatureImageView)
            make.height.width.greaterThanOrEqualTo(0)
            
        }
        //title label
        contentBackView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)

        }
        subTitleLabel.numberOfLines = 2
        subTitleLabel.adjustsFontSizeToFitWidth = true
        contentBackView.addSubview(subTitleLabel)
        subTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.equalTo(titleLabel)
            make.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()

        }
        //more image view
        let moreImageView = ViewFactory.shared.generateMoreImageView()
        backView.addSubview(moreImageView)
        moreImageView.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right)
            make.right.equalTo(-16)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
        //result label
        //抗压缩,抗拉伸能力最强，不会被压缩或拉伸
        resultLabel.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        resultLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        backView.addSubview(resultLabel)
        resultLabel.snp.makeConstraints { make in
            make.left.equalTo(contentBackView.snp.right).offset(5)
            make.right.equalTo(moreImageView.snp.left)
            make.height.equalTo(40)
            make.width.greaterThanOrEqualTo(0)
            make.centerY.equalToSuperview()
        }
    }
    func setData(_ model: PVDChecklistModel, lastRowFlag: Bool = false){
        guard let signType = SignatureType(rawValue: model.type) else { return  }
        if lastRowFlag {//最后一行
            self.backView.drawCorner(byRoundingCorners: [.bottomLeft,.bottomRight])
        }else{//没有下一行
            self.backView.drawCorner(byRoundingCorners: [])
        }
        //
        self.isRequiredImageView.isHidden = (model.is_required == "true" ? false : true)//只有required=true才显示
        //signature type
        titleLabel.text = signType.rawValue
        subTitleLabel.text = model.title
        //
        switch signType {
        case .InstallerAndDesiger:
            if JobDetailModelHelper.shared.jobDetailModel.signatures.installer_signed_at != "" && JobDetailModelHelper.shared.jobDetailModel.signatures.designer_signed_at != "" {
                self.resultLabel.text = "Done"
            }
        case .Installer:
            if JobDetailModelHelper.shared.jobDetailModel.signatures.installer_signed_at != ""{
                self.resultLabel.text = "Done"
            }
        case .Designer:
            if JobDetailModelHelper.shared.jobDetailModel.signatures.designer_signed_at != ""{
                self.resultLabel.text = "Done"
            }
        case .Owner:
            if JobDetailModelHelper.shared.jobDetailModel.signatures.owner_signed_at != ""{
                self.resultLabel.text = "Done"
            }
        case .Witness:
            if JobDetailModelHelper.shared.jobDetailModel.signatures.witness_signed_at != ""{
                self.resultLabel.text = "Done"
            }
        default:
            break
        }
        uploadFlagImageView.image = (model.localDataflag ? R.image.uploadImageFailed() : nil)
    }
}
